<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="景点名称" prop="spotName">
          <el-input
            v-model="queryParams.spotName"
            placeholder="请输入要去的景点名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="routeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="路线编号" prop="route.routeId" width="120" />
        <el-table-column label="路线途径景点" prop="spotNames" width="400" />
        <el-table-column label="路线描述" prop="route.description" :show-overflow-tooltip="true" width="150" />
        
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleQueryById(scope.row)"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="路线详情" :visible.sync="openView" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="路线编号：">{{ form.routeId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路线描述：">{{ form.description }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="景点观光顺序：">{{ formattedRoute }}</el-form-item>
          </el-col>
      </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>

    
  </div>
</template>

<script>
import { listRoute, getRouteDetailByRouteId } from "@/api/tourss/route/route";

export default {
  name: "Route",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      loading: true,
      routeList: [],
      total: 0,
      //是否显示搜索框
      showSearch: true,
      //是否显示景区详情层
      openView: false,
      //景点详情参数
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spotName: undefined,
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    formattedRoute() {
      return this.getFormattedRoute(this.form.spotNames);
    }
  },
  methods: {
    /** 查询景点列表 */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then(response => {
          this.routeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleQueryById(row){
      console.log(row);
      getRouteDetailByRouteId(row.route.routeId).then(response => {
        console.log("----------------------");
        console.log(response.data);
        this.form = response.data;
        this.openView = true;
      });
    },
    getFormattedRoute(spotNames) {
      if (!Array.isArray(spotNames)) {
        return '';
      }
      return spotNames.map((spot, index) => {
        return index < spotNames.length - 1 ? `${spot} ->` : spot;
      }).join(' ');
    }
  }
};
</script>